﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame4
{
    class Player_DestroyState: State
    {
        private static Player_DestroyState instance;

        private Player_DestroyState()
        {
            base.Name = "DestroyState";
        }

        public override void enter(AIObject aiObject)
        {
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            GameInfo gameInfo = GameInfo.getInstance();

            if (player.Blinking == false)
            {
                // Decrement lives
                player.Lives--;
                // Put to the initial point
                int rowVictim = gameInfo.World.getNumRow(player.Position.Y);
                int columnVictim = gameInfo.World.getNumColumn(player.Position.X);
                player.Position = new Position2D(player.InitialPosition.X, player.InitialPosition.Y);
                /*
                int initialRow = gameInfo.World.getNumRow(player.InitialPosition.Y);
                int initialColumn = gameInfo.World.getNumColumn(player.InitialPosition.X);
                gameInfo.World.get(initialRow, initialColumn).Obj = player;
                gameInfo.World.get(rowVictim, columnVictim).Obj = null;*/
                player.Movement = null;
                player.Route = null;
                EventEmitter.getInstance().registerMessage(0, player.Id, player.Id, Message.MessageType.msg_create);

                if (player.Lives <= 0)
                {
                    player.Lives = 0;
                    // Remove object
                    gameInfo.removeObject(player.Id, player.Type);
                }
                else
                {
                    // Create star
                    gameInfo.createRandomStar(player.Level - player.InitalLevel);
                    // Remove level
                    player.Level = player.InitalLevel;

                }
            }
            else
            {
                EventEmitter.getInstance().registerMessage(0, player.Id, player.Id, Message.MessageType.msg_stop);
            }
        }

        public static Player_DestroyState getInstance()
        {
            if (instance == null)
                instance = new Player_DestroyState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
        }

        public override void exit(AIObject aiObject)
        {
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_create:
                    {
                        aiObject.StateMachine.changeState(Player_InitialState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_stop:
                    {
                        aiObject.StateMachine.changeState(Player_StopState.getInstance());
                        correct = true;
                        break;
                    }
            }
            return correct;
        }
    }
}
